<?php
namespace App\Controllers;
class ixform extends \CodeIgniter\Controller
{
 public function index()
 {
 // connect to the model
 $places = new \App\Models\ixformModels();
 // retrieve all the records
 $records = $places->findAll();
 // JSON encode and return the result
          $table = new \CodeIgniter\View\Table();
        $headings = ["Name","Image","Link"];
        $table->setHeading($headings);
        
        foreach ($records as $record) {        
            $editLink =anchor("ixform/showme/" . $record['id'], "Details");
            $imgString = "<img src=\"/image/" . $record['image'] . "\"/>";
            $table->addRow($record['name'],['data' => $imgString],$editLink);
        }
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        
         $table->setTemplate($template);
         
          $fields = [
            'title' => 'ixform Member',
            'heading' => 'ixform Member',
            'footer' => 'Copyright lys'
        ];

// get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
      
 }
     public function showme($id) {
        // connect to the model
        $models = new \App\Models\ixformModels();
        $table = new \CodeIgniter\View\Table();
        $headings = $models->fields;
        // retrieve all the records
        $record = $models->find($id);
        //$table->addRow(anchor("travel/edit/".$record['id'],"Edit"));
        $table->addRow(ucfirst($headings[0]), $record['id']);
        $table->addRow(ucfirst($headings[1]), $record['name']);
        $table->addRow(ucfirst($headings[2]), $record['height']);
        $table->addRow(ucfirst($headings[3]), $record['birth']);
        $table->addRow(ucfirst($headings[4]), $record['country']);
        $table->addRow(ucfirst($headings[5]), $record['position']);
        $table->addRow(ucfirst($headings[6]), $record['blood']);
        $table->addRow(ucfirst($headings[7]), anchor($record['link'], $record['link']));
        $imgString = "<img src=\"/image/" . $record['image'] . "\"/>";
        $table->addRow(['data' => $imgString, 'colspan' => 2]);
        $parser = \Config\Services::parser();
        $fields = [
            'title' => 'Ixform Member',
            'heading' => 'Ixform '. $record['name'],
            'footer' => 'Copyright lys'
        ];

        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];

        $table->setTemplate($template);
// tell it about the substitions
        return $parser->setData($fields)
                        ->render('templates\top') .
                $parser->renderString(anchor("ixform/edit/" . $record['id'], "Edit")) .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }
     public function edit($id = NULL) {
        $models = new \App\Models\ixformModels();
        $record = $models->find($id);

        $fields = [
            'title' => 'Edit' . $record['name'],
            'heading' => 'Edit' . $record['name'],
            'footer' => 'Copyright Tina'
        ];
          
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
                        ->render('templates\top') .
                        $parser->setData($record)
                        ->render('editseventeen') .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }
public function handle() {
        $models = new \App\Models\ixformModels();
        helper(['form', 'url']);
        $record = $models->find($this->request->getPost('id'));

        if ($this->validate($models->validationRules)) {
            $models->save([
                'id' => $this->request->getPost('id'),
                'name' => $this->request->getPost('name'),
                'height' => $this->request->getPost('height'),
                'birth' => $this->request->getPost('birth'),
                'country' => $this->request->getPost('country'),
                'position' => $this->request->getPost('position'),
                'birth' => $this->request->getPost('birth'),
                'blood' => $this->request->getPost('blood'),
                'link' => $this->request->getPost('link'),
                'image' => $this->request->getPost('image'),
            ]);
        }

        $fields = [
            'title' => 'Edit' . $record['name'],
            'heading' => 'Edit' . $record['name'],
            'footer' => 'Copyright lys'
        ];

        $parser = \Config\Services::parser();

        $returnView = $parser->setData($fields)->render('templates\top');

        if (!$this->validate($models->validationRules)) {
            $returnView .= $parser->renderString($this->validator->listErrors());
            $returnView .= $parser->setData($this->request->getPost())->render('editixform');
        } else {
            $returnView .= $parser->renderString("<p>Success!</p>");
            $returnView .= $parser->setData($record)->render('editixform');
        }
        $returnView .= $parser->setData($fields)->render('templates\bottom');
        return $returnView;
    }
}
 //return json_encode($records);
 

